
"use strict";
angular.module('app.project').controller('Add_newProjectCtrl', function ($scope,$location, $stateParams, $http, $cookieStore) {

    $scope.infor = {}; // 项目基本信息mode
    $scope.pro_id = $location.search().pro_id;
    $scope.userid = $cookieStore.get('userid');
    $scope.type_obj = {};
    // 设备类型
    $http.get(baseurl+'/device_types').then(function(res){
        // console.log(res.data);
        if (res.data.ret == 1) {
            $scope.types = res.data.info;
            if (res.data.info.length > 0) {
                $scope.first_type = res.data.info[0].id;
                for (var i = 0; i < res.data.info.length; i++) {
                    var obj = res.data.info[i];
                    $scope.type_obj[obj.id] = obj.name;
                }

            }
        }
    });
    // 端口号（哪种协议）
    $http.get(baseurl+'/select_ports').then(function(res){
        // console.log(res.data);
        if (res.data.ret == 1 && res.data.info != null) {
            $scope.ports = res.data.info;
            if (res.data.info.length > 0) {
                $scope.first_port = res.data.info[0].port;
            }
        }
    });
    // 获取该项目的信息
    if ($scope.pro_id) {
        $scope.delbtn = false;
        $http.get(baseurl+"/detail/project?id=="+$scope.pro_id).then(function(res){
            if (res.data.ret == 1) {
                $scope.infor = res.data.info;
            }
        })
    }else{
        $scope.delbtn = true;//隐藏
    }
    // 保存该项目
    $scope.save_infor = function () { // 点击保存// 判断是创建新项目还是编辑项目
        if ($scope.pro_id) {
            if($scope.infor.name && $scope.infor.project_num && $scope.infor.clientname){
                $http.put(baseurl+'/data/project?id=='+$scope.pro_id,$scope.infor).then(function(res){
                    if (res.data.ret == 1) {
                        window.location.href = "/#/myallprojects/";//编辑项目
                    }
                });
            }else{
                new Notify("fail","有空值未填写");
            }

        }else{
            if($scope.infor.name && $scope.infor.project_num && $scope.infor.clientname){
                $scope.infor.fid = $scope.userid;
                $http.post(baseurl+'/data/project',$scope.infor).then(function(res){
                    if (res.data.ret == 1) {
                        window.location.href = "/#/myallprojects/";//添加项目
                    }
                });
            }else{
                new Notify("fail","有空值未填写");
            }

        }

    };
    // 设备列表的获取
    $scope.cur_page = 1;
    $scope.cur_rows = 9;
    // 只属于项目的设备
    $scope.select = function (zu_id) {
        var page_obj = {};
        page_obj.page = $scope.cur_page;
        page_obj.rows = $scope.cur_rows;
        var projectid = $scope.pro_id;
        var query = "?projectid=="+projectid+"&groupid=="+zu_id+"";
        $http.post(baseurl+'/list/select/view_device_project'+query,page_obj).then(function(res){
            if (res.data.ret === 1) {
                if (res.data.rows !== null) {
                    for (var i = 0; i < res.data.rows.length; i++) {
                        var obj = res.data.rows[i];
                        obj.typename = $scope.type_obj[obj.type];
                        if (obj.status === '0') {
                            obj.statusname = '异常';
                            obj.class_status = 'status_error';
                        }else{
                            obj.statusname = '正常';
                            obj.class_status = 'status_normal';
                        }
                    }
                    if (res.data.rows < 1 && $scope.more_data === true) {
                        new Notify('info','没有更多数据了');
                    }else{
                        if ($scope.cur_page === 1){
                            $scope.datas = res.data.rows;
                        }else{
                            var a = $scope.datas;
                            var c = a.concat(res.data.rows);
                            $scope.datas = c;
                        }
                    }
                }
            }
        })
    };
    if ($scope.pro_id && $scope.pro_id != '') {
        $scope.select(0);
    }
    // 加载更多
    $scope.jiazai_more = function(){
        $scope.cur_page += 1;
        $scope.more_data = true;
        $scope.select(0,$scope.datas);
    };

    $scope.select1 = function (zu_id) {
        var page_obj = {};
        page_obj.page = $scope.cur_page;
        page_obj.rows = $scope.cur_rows;
        var projectid = $scope.pro_id;
        var query = "?projectid=="+projectid+"&groupid=="+zu_id+"";
        $http.post(baseurl+'/list/select/view_device_project'+query,page_obj).then(function(res){
            if (res.data.ret === 1) {
                if (res.data.rows !== null) {
                    for (var i = 0; i < res.data.rows.length; i++) {
                        var obj = res.data.rows[i];
                        obj.typename = $scope.type_obj[obj.type];
                        if (obj.status === '0') {
                            obj.statusname = '异常';
                            obj.class_status = 'status_error';
                        }else{
                            obj.statusname = '正常';
                            obj.class_status = 'status_normal';
                        }
                    }
                    if (res.data.rows < 1 && $scope.more_data === true) {
                        new Notify('info','没有更多数据了');
                    }else{
                        if ($scope.cur_page === 1){
                            $scope.datas_children = res.data.rows;
                            if(res.data.rows.length === 0){
                                new Notify('info','暂无设备');
                            }
                        }else{
                            var a = $scope.datas_children;
                            var c = a.concat(res.data.rows);
                            $scope.datas_children = c;
                        }
                    }
                }
            }
        })
    };
    // 添加设备分组
    $scope.add_list_zu = function () {
        var data = {};
        data.projectid = $scope.pro_id;
        data.name = '请点击修改名称';
        $http.post(baseurl+"/data/group1",data).then(function(res){
            if(res.data.ret === 1){
                get_list_zu();
            }else{
                new Notify('info','您还有未命名的组');
            }
        })
    };

    // 获取所有设备分组
    function get_list_zu() {
        $http.get(baseurl+"/list_nopage/group1?projectid=="+$scope.pro_id).then(function(res){
            if(res.data.ret === 1){
                $scope.list_zu = res.data.info;
            }
        });
    }
    get_list_zu();

    // 设备分组每次只能打开一个
    $scope.show_hide = function (index,data) {
        $scope.datas_children = null;
        $scope.select1(data.id);
        $scope.delete_dev_end = data.id;
        $(".dev_list_content").hide();
        $(".dev_list_content").eq(index).fadeIn();
        $scope.cur_page = 1;
        $scope.cur_rows = 9;
    };

    // 删除组
    $scope.delete_zu = function(data){
        $('#showDelDialog').modal('show');
        $scope.delete_zu_id = data.id;
    };

    $scope.delete_zu_sure = function(){
        var page_obj = {};
        page_obj.page = 1;
        page_obj.rows = 9;
        var projectid = $scope.pro_id;
        var query = "?projectid=="+projectid+"&groupid=="+$scope.delete_zu_id+"";
        $http.post(baseurl+'/list/select/view_device_project'+query,page_obj).then(function(res){
            if(res.data.ret === 1){
                if(res.data.rows.length > 0){
                    new Notify('info','分组有设备不能删除');
                }else{
                    $http.delete(baseurl+"/data/group1?id="+$scope.delete_zu_id+"").then(function(res){
                        if(res.data.ret === 1){
                            new Notify('success','删除成功');
                            get_list_zu();
                            $('#showDelDialog').modal('hide');
                        }
                    })
                }
            }
        });

    };

    // 修改设备分组的名称
    $scope.show_hide1 = function (index) {
        var val = $(".dev_list_name").eq(index).text();
        $(".dev_list_name").eq(index).hide();
        $(".Edit_list_name").eq(index).children('.ipt_name').val(val);
        $(".Edit_list_name").eq(index).show();
    };

    // 确定修改设备分组的名称
    $scope.show_hide2 = function (index,info) {
        var val = $(".sure_name").eq(index).prev().val();

        var data = {name:val};
        $http.put(baseurl+"/data/group1?id="+info.id+"",data).then(function(res){
            if(res.data.ret === -1){
                new Notify('info','请勿输入重复组名');
            }else {
                $(".sure_name").eq(index).parent().hide();
                $(".dev_list_name").eq(index).show();
                $(".dev_list_name").eq(index).text(val);
            }
        })
    };

    // 取消修改设备分组的名称
    $scope.show_hide3 = function (index) {
        $(".no_name").eq(index).parent().hide();
        $(".dev_list_name").eq(index).show();
    };
    $scope.show_addbox = function(info,index){
        $scope.add_device_id = parseInt(info);
        $scope.suibian = parseInt(info);
        $scope.list_index = index
    };
    // 添加设备
    $scope.insertdata = function () {
        if ($scope.insert.clientname == null || $scope.insert.clientname == '') {
            new Notify('info','客户名称不为空');
            return;
        }
        if ($scope.insert.devicename == null || $scope.insert.devicename == '') {
            new Notify('info','设备名称不为空');
            return;
        }
        if ($scope.insert.deviceid == null || $scope.insert.deviceid == '') {
            new Notify('info','设备号不为空');
            return;
        }
        if ($scope.insert.port == null || $scope.insert.port == '') {
            new Notify('info','端口号不为空');
            return;
        }
        $scope.insert.projectid = $scope.pro_id;
        $scope.insert.groupid = $scope.add_device_id;
        $http.post(baseurl+'/data/device',$scope.insert).then(function(res){
            if (res.data.ret == 1) {
                if($scope.add_device_id === 0){
                    $scope.select($scope.add_device_id);
                }else{
                    $(".dev_list_content").hide();
                    $(".dev_list_content").eq($scope.list_index).fadeIn();
                    $scope.select1($scope.add_device_id);
                }
                $scope.cur_page = 1;
                $scope.cur_rows = 9;
                $scope.delete_dev_end =   $scope.suibian
                new Notify('success','添加成功');
                $("#addModal").modal("hide");
            }else if (res.data.ret != -4){
                new Notify('fail','设备号已存在');
            }
        });
    };

    // 编辑设备框
    $scope.editdata = {};
    $scope.showEditDevice = function (data,info) {
        $scope.editid = data.id;
        $scope.editdata.clientname = data.clientname;
        $scope.editdata.devicename = data.devicename;
        $scope.editdata.deviceid = data.deviceid;
        $scope.editdata.port = data.port;
        $scope.editdata.zone = data.zone;
        $scope.editdata.pos = data.pos;
        $scope.editdata.projectid = $scope.pro_id;
        $scope.editdata.type = data.type;
        $('#editModal').modal('show');

        $scope.editzu_id = parseInt(info);
    };
    // 确认编辑
    $scope.updatedata = function () {
        if ($scope.editdata.clientname == null || $scope.editdata.clientname == '') {
            new Notify('info','客户名称不为空');
            return;
        }
        if ($scope.editdata.devicename == null || $scope.editdata.devicename == '') {
            new Notify('info','设备名称不为空');
            return;
        }
        if ($scope.editdata.deviceid == null || $scope.editdata.deviceid == '') {
            new Notify('info','设备号不为空');
            return;
        }
        if ($scope.editdata.port == null || $scope.editdata.port == '') {
            new Notify('info','端口号不为空');
            return;
        }
        $http.put(baseurl+'/data/device?id=='+$scope.editid,$scope.editdata).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
                new Notify('success','修改成功');
                $("#editModal").modal("hide");
                if($scope.editzu_id === 0){
                    $scope.select($scope.editzu_id)
                }else {
                    $scope.select1($scope.editzu_id);
                }
            }
        });
    };

    // 删除设备框
    var del_ele = null;
    $scope.show_dev_del = function(id){
        $scope.del_dev_id= id;
        $("#showdev").modal('show')
    };
    $scope.deldevice = function () {
        $http.delete(baseurl+'/data/device?id=='+$scope.del_dev_id).then(function(res){
            if (res.data.ret == 1) {
                new Notify('success','删除成功');
                $('#showdev').modal('hide');
                $scope.select1($scope.delete_dev_end);
                $scope.select(0);
            }
        });
    }
});